export default {
    permutator(inputArr) {
        let results = [];

        function permute(arr, memo) {
            memo = memo || [];
            var cur = memo

            for (var i = 0; i < arr.length; i++) {
                cur = arr.splice(i, 1);
                if (arr.length === 0) {
                    results.push(memo.concat(cur));
                }
                permute(arr.slice(), memo.concat(cur));
                arr.splice(i, 0, cur[0]);
            }

            return results;
        }

        return permute(inputArr);
    },

    /**
     * [组合运算]
     * @param  {array} arr [进行组合运算的数组]
     * @example [1,3,2]
     * @param  {number} num [每几个元素为一组]
     * @return {array}     [返回组合集合]
     * @example [[1,3],[1,2],[3,2]]
     */
    combo(arr, num) {
        var result = [];
        var range = function(r, _arr) {
            if (r.length == num) {
                result.push(r)
            } else {
                let l = r.length;
                for (var i = 0, len = _arr.length - num + l; i <= len; i++) {
                    range(r.concat(_arr[i]), _arr.slice(i + 1))
                }
            }
        }
        range([], arr);
        return result
    },

    addToLib(ques) {
        let quesLib = localStorage.getItem('quesLib');
        if(!quesLib) {
            quesLib = []
        } else {
            quesLib = JSON.parse(quesLib)
        }
        quesLib.push(ques)
        localStorage.setItem('quesLib', JSON.stringify(quesLib))
    }
}
